package inutan.game.proto;

import inutan.data.Resource.Type;
import inutan.game.entity.prop.BaseItemInfo;
import inutan.game.entity.prop.CardInfo;
import luban.item.*;
import org.ricks.net.DataCodecKit;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Explain -->
 * @Author yanwang
 * @Date 2023/4/11 10:54
 **/
public class ItemProto {

    public static ResItemInfoList resItemInfoList(List<BaseItemInfo> itemInfoList) {
        ResItemInfoList info = new ResItemInfoList();
        info.list = new ArrayList<>();
        Map<Integer, List<BaseItemInfo>> map = new HashMap<>();
        itemInfoList.forEach(x -> {
            if (map.containsKey(x.getItemType())) {
                map.get(x.getItemType()).add(x);
            } else {
                List<BaseItemInfo> list = new ArrayList<>();
                list.add(x);
                map.put(x.getItemType(), list);
            }
        });
        map.forEach((key, value) -> {
            info.list.add(resItemInfo(key, value));
        });
        return info;
    }

    public static ResItemInfo resItemInfo(int type, List<BaseItemInfo> itemInfoList) {
        ResItemInfo resItemInfo = new ResItemInfo();
        resItemInfo.itemType = type;
        resItemInfo.itemInfo = new ArrayList<>();
        itemInfoList.stream().filter(x -> x.getItemType() == type).forEach(x -> {
            resItemInfo.itemInfo.add(getItemInfo(x));
        });
        return resItemInfo;
    }

    public static luban.item.ItemInfo getItemInfo(BaseItemInfo item) {
        luban.item.ItemInfo itemInfo = new luban.item.ItemInfo();
        itemInfo.id = item.getId();
        itemInfo.itemId = item.getItemId();
        itemInfo.count = item.getCount();
        return itemInfo;
    }


    public static CardInfo getCardInfo(BaseItemInfo itemInfo) {
        CardInfo card = (CardInfo) itemInfo;
        if(Objects.isNull(card)) {
            return null;
        }
        CardInfo cardInfo = new CardInfo();
        cardInfo.setItemId(itemInfo.getItemId());
        cardInfo.exp = card.exp;
        cardInfo.lv = card.lv;
        return cardInfo;
    }

    public static ChangeItemInfo getChangeItemInfo(BaseItemInfo itemInfo) {
        ChangeItemInfo changeItemInfo = new ChangeItemInfo();
        if(Objects.isNull(changeItemInfo)) {
            return null;
        }
        changeItemInfo.id = itemInfo.getId();
        changeItemInfo.itemId = itemInfo.getItemId();
        changeItemInfo.count = itemInfo.getCount();
        changeItemInfo.attached = getItemAttached(itemInfo);
        return changeItemInfo;
    }

    public static ChangeItemInfos getChangeItemInfos(List<BaseItemInfo> itemInfos) {
        List<ChangeItemInfo> list = itemInfos.stream().filter(Objects::nonNull).map(x -> getChangeItemInfo(x)).filter(Objects::nonNull).collect(Collectors.toList());
        ChangeItemInfos changeItemInfos = new ChangeItemInfos();
        changeItemInfos.changeItem = new ArrayList<>();
        changeItemInfos.changeItem.addAll(list);
        return changeItemInfos;
    }

    public static byte[] getItemAttached(BaseItemInfo itemInfo) {
        byte[] bytes;
        switch (itemInfo.getItemType()) {
            case Type.CHARACTER:
                CardInfo cardInfo = getCardInfo(itemInfo);
                if(Objects.isNull(cardInfo)) {
                    bytes = new byte[0];
                    break;
                }
                bytes = DataCodecKit.encode(cardInfo);
                break;
            default:
                bytes = new byte[0];
                break;
        }
        return bytes;
    }
}
